package com.gitee.zycra.union.api.dao;

import com.gitee.zycra.jdbc.common.BaseDao;
import com.gitee.zycra.jdbc.common.CommonSelector;
import com.gitee.zycra.union.api.common.model.ApiUserGroup;
import com.gitee.zycra.union.api.common.vo.ProjectGroupVO;
import com.gitee.zycra.union.api.common.vo.UserAuthVO;
import com.gitee.zycra.union.api.common.vo.UserInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author zycra
 */
@Repository
public class ApiUserGroupDao extends BaseDao<ApiUserGroup> {

    @Autowired
    private CommonSelector commonSelector;

    /**
     * 插入
     *
     * @param apiUserGroup 用户组对象
     * @return 插入条数
     */
    public int insertApiUserGroup(ApiUserGroup apiUserGroup) {
        return insertIntoTable(apiUserGroup);
    }

    /**
     * 更新
     *
     * @param apiUserGroup 用户组对象
     * @return 更新条数
     */
    public int updateApiUserGroup(ApiUserGroup apiUserGroup) {
        return updateTableById(apiUserGroup);
    }

    /**
     * 更新为删除
     *
     * @param groupId 项目组ID
     * @param userId  用户ID
     * @return 更新条数
     */
    public int updateToDelete(Long groupId, Long userId) {
        return getJdbcTemplate().update("UPDATE api_user_group SET deleted = 1 WHERE user_id = ? AND group_id = ?",
                List.of(userId, groupId).toArray());
    }

    /**
     * 更新为删除
     *
     * @param groupId 项目组ID
     * @param userId  用户ID
     * @param auth    权限
     * @return 更新条数
     */
    public int updateAuth(Long groupId, Long userId, Integer auth) {
        return getJdbcTemplate().update("UPDATE api_user_group SET auth = ? WHERE user_id = ? AND group_id = ? AND deleted = 0",
                auth, userId, groupId);
    }

    /**
     * 移除项目组内用户
     *
     * @param groupId 项目组ID
     * @param userId  用户ID
     * @return 影响行数
     */
    public int removeUser(Long groupId, Long userId) {
        return getJdbcTemplate().update("UPDATE api_user_group SET deleted = 1 WHERE user_id = ? AND group_id = ?", userId, groupId);
    }

    /**
     * 根据用户ID查询
     *
     * @param userId 用户ID
     * @return 查询到的数据
     */
    public List<UserAuthVO> selectUserAuthByUserId(Long userId) {
        return commonSelector.selectByParam(getJdbcTemplate(), "SELECT aug.auth, apg.group_name FROM api_user_group aug INNER JOIN "
                        + "api_project_group apg ON aug.group_id = apg.id AND aug.deleted = 0 AND apg.deleted = 0 WHERE aug.user_id = ?",
                UserAuthVO.class, List.of(userId));
    }

    /**
     * 根据用户ID和项目组ID查询权限
     *
     * @param userId  用户ID
     * @param groupId 组ID
     * @return 权限
     */
    public String selectAuthByUserIdAndGroupId(Long userId, Long groupId) {
        return commonSelector.selectOneByParam(getJdbcTemplate(), "SELECT auth FROM api_user_group WHERE deleted = 0 "
                + "AND user_id = ? AND group_id = ?", String.class, List.of(userId, groupId));
    }

    /**
     * 根据项目组ID查询其他组非超级管理员用户
     *
     * @param groupId 项目组ID
     * @return 用户列表
     */
    public List<UserInfoVO> selectUserInfoForAddGroup(Long groupId) {
        return commonSelector.selectByParam(getJdbcTemplate(), "SELECT id, username FROM api_user WHERE super_admin = 0 AND enabled = 1 "
                + "AND id NOT IN(SELECT user_id FROM api_user_group WHERE deleted = 0 AND group_id = ?)", UserInfoVO.class, List.of(groupId));
    }

    /**
     * 根据用户ID查询项目组
     *
     * @param userId 用户ID
     * @return 项目组集合
     */
    public List<ProjectGroupVO> selectUserGroups(Long userId) {
        return commonSelector.selectByParam(getJdbcTemplate(), "SELECT apg.id, apg.group_name FROM api_project_group apg INNER JOIN "
                        + "api_user_group aug ON apg.id = aug.group_id AND apg.deleted = 0 AND aug.deleted = 0 WHERE aug.user_id = ?",
                ProjectGroupVO.class, List.of(userId));
    }
}
